class Solution {
public:
	ListNode * deleteDuplicates(ListNode* head) {
		if (head == NULL || head->next == NULL)
			return head;

		std::unordered_map<int, int> valuecount;
		ListNode* node = head;
		while (node != NULL){
			valuecount[node->val] += 1;
			node = node->next;
		}

		node = head;
		ListNode* node1 = NULL;
		head = NULL;
		while (node != NULL){
			if (head == NULL){
				if (valuecount[node->val] > 1) {
					node = node->next;
				}else{
					head = node;
					node1 = head;
				}
			}else{
				if (valuecount[node->val] > 1) {
					node1->next = node->next;
					node = node1->next;
				}
				else {
					node1 = node;
					node = node->next;
				}	
			}		
		}
		return head;
	}
};